/*****************************************************************************
*        描述 :
*             文件描述
*        CLR版本:            3.5
*        创建者 :            蒋 光
*        个人域名:           jiangguang.net.cn
*        命名空间名称:       ECP.DataAccess
*        文件名:             BaseWorkFlowStepDao.cs
*        创建系统时间:       2012/11/2 19:07:00
*        创建年份:           2012
*
/*****************************************************************************/

namespace ECP.DataAccess
{
    using System.Data;
    using ECP.DataAccess.Utilities;
    using ECP.Model;
    using ECP.Utilities;

    public class BaseWorkFlowStepDao : BaseDao, IBaseDao
    {
        public BaseWorkFlowStepDao()
        {
            base.CurrentTableName = BaseWorkFlowStepTable.TableName;
        }

        public BaseWorkFlowStepDao(IDbHelper dbHelper, BaseUserInfo userInfo)
        {
            base.DbHelper = dbHelper;
            base.UserInfo = userInfo;
        }

        public string Add(BaseWorkFlowStepEntity workFlowStepEntity)
        {
            string str = string.Empty;
            str = this.AddEntity(workFlowStepEntity);
            base.ReturnStatusCode = StatusCode.OKAdd.ToString();
            base.ReturnStatusMessage = base.GetStateMessage(base.ReturnStatusCode);
            return str;
        }

        public string AddEntity(BaseWorkFlowStepEntity workFlowStepEntity)
        {
            string sequence = BaseSequenceDao.Instance.GetSequence(base.DbHelper, BaseWorkFlowStepTable.TableName);
            SQLBuilder builder = new SQLBuilder(base.DbHelper);
            builder.BeginInsert(BaseWorkFlowStepTable.TableName);
            builder.SetValue(BaseWorkFlowStepTable.FieldID, sequence);
            builder.SetValue(BaseWorkFlowStepTable.FieldFullName, workFlowStepEntity.FullName);
            builder.SetValue(BaseWorkFlowStepTable.FieldFlowID, workFlowStepEntity.FlowID);
            builder.SetValue(BaseWorkFlowStepTable.FieldAudiUserID, workFlowStepEntity.AudiUserID);
            builder.SetValue(BaseWorkFlowStepTable.FieldAudiRoleID, workFlowStepEntity.AudiRoleID);
            builder.SetValue(BaseWorkFlowStepTable.FieldAudiDutyID, workFlowStepEntity.AudiDutyID);
            builder.SetValue(BaseWorkFlowStepTable.FieldSortCode, sequence);
            builder.SetValue(BaseWorkFlowStepTable.FieldProperty, workFlowStepEntity.Property);
            builder.SetValue(BaseWorkFlowStepTable.FieldEnabled, workFlowStepEntity.Enabled ? 1 : 0);
            builder.SetValue(BaseWorkFlowStepTable.FieldDescription, workFlowStepEntity.Description);
            builder.SetValue(BaseWorkFlowStepTable.FieldCreateUserID, base.UserInfo.ID);
            builder.SetDBNow(BaseWorkFlowStepTable.FieldCreateDate);
            return ((builder.EndInsert() > 0) ? sequence : string.Empty);
        }

        public DataSet GetFlowStep(string flowID)
        {
            string sqlQuery = " SELECT W.ID       , W.STEP       , W.FLOWID       , W.FULLNAME       , W.PROPERTY       , W.AUDISTAFFID       , W.AUDIROLEID       , W.AUDIDUTYID       , W.SORTCODE       , W.ENABLED       , W.DESCRIPTION       , W.CREATESTAFFID       , W.CREATEDATE       , W.MODIFYSTAFFID       , W.MODIFYDATE " + BaseStaffTable.TableName + " WHERE ID = W.AUDISTAFFID) AS AUDISTAFFFULLNAME       ,(SELECT FULLNAME FROM " + BaseRoleTable.TableName + " WHERE ID = W.AUDIROLEID) AS AUDIROLEFULLNAME       ,(SELECT FULLNAME FROM " + BaseItemDetailsTable.TableName + " WHERE ID = W.AUDIDUTYID) AS AUDIDUTYFULLNAME 　FROM " + BaseWorkFlowStepTable.TableName + " W  WHERE FlowID = ?   ORDER BY W.SORTCODE ";
            DataSet dataSet = new DataSet();
            base.DbHelper.Fill(dataSet, sqlQuery, BaseWorkFlowStepTable.TableName, base.DbHelper.MakeParameters(BaseWorkFlowStepTable.FieldFlowID, flowID));
            return dataSet;
        }

        public DataTable GetList(string id)
        {
            return DbLogic.GetDT(base.DbHelper, BaseWorkFlowStepTable.TableName, BaseWorkFlowStepTable.FieldFlowID, id);
        }

        public int Update(BaseWorkFlowStepEntity workFlowStepEntity)
        {
            int num = 0;
            if (DbLogic.IsModifed(base.DbHelper, BaseWorkFlowStepTable.TableName, workFlowStepEntity.ID, workFlowStepEntity.ModifyUserID, workFlowStepEntity.ModifyDate))
            {
                base.ReturnStatusCode = StatusCode.ErrorChanged.ToString();
            }
            else
            {
                num = this.UpdateEntity(workFlowStepEntity);
                if (num == 1)
                {
                    num = this.UpdateEntity(workFlowStepEntity);
                    base.ReturnStatusCode = StatusCode.OKUpdate.ToString();
                }
                else
                {
                    base.ReturnStatusCode = StatusCode.ErrorDeleted.ToString();
                }
            }
            base.ReturnStatusMessage = base.GetStateMessage(base.ReturnStatusCode);
            return num;
        }

        public int UpdateEntity(BaseWorkFlowStepEntity workFlowStepEntity)
        {
            SQLBuilder builder = new SQLBuilder(base.DbHelper);
            builder.BeginUpdate(BaseWorkFlowStepTable.TableName);
            builder.SetValue(BaseWorkFlowStepTable.FieldFullName, workFlowStepEntity.FullName);
            builder.SetValue(BaseWorkFlowStepTable.FieldFlowID, workFlowStepEntity.FlowID);
            builder.SetValue(BaseWorkFlowStepTable.FieldAudiUserID, workFlowStepEntity.AudiUserID);
            builder.SetValue(BaseWorkFlowStepTable.FieldAudiRoleID, workFlowStepEntity.AudiRoleID);
            builder.SetValue(BaseWorkFlowStepTable.FieldAudiDutyID, workFlowStepEntity.AudiDutyID);
            builder.SetValue(BaseWorkFlowStepTable.FieldProperty, workFlowStepEntity.Property);
            builder.SetValue(BaseWorkFlowStepTable.FieldEnabled, workFlowStepEntity.Enabled ? 1 : 0);
            builder.SetValue(BaseWorkFlowStepTable.FieldDescription, workFlowStepEntity.Description);
            builder.SetValue(BaseWorkFlowStepTable.FieldModifyUserID, base.UserInfo.ID);
            builder.SetDBNow(BaseWorkFlowStepTable.FieldModifyDate);
            builder.SetWhere(BaseWorkFlowStepTable.FieldID, workFlowStepEntity.ID);
            return builder.EndUpdate();
        }
    }
}